<?php

namespace app\uni\controller;


use think\Controller;

// 微信公众号封装类
use EasyWeChat\Factory;

class Order extends Controller
{
    public function _initialize()
    {
        $this->OrderModel = model('Order.Order');
    }

    // 首页订单列表
    public function sylist(){
      // 首页预约情况 日期、航线id、时间段id
      $date = $this->request->param('date', '', 'trim');
      $time_id = $this->request->param('time_id', '', 'trim');
      $airline_id = $this->request->param('airline_id', '', 'trim');

      $list=$this->OrderModel
      ->with(['airline','startPoint','endPoint','type','timelist'])
      ->where(['ps_date'=>$date,'ps_time_id'=>$time_id,'order.airline_id'=>$airline_id])->order('end_point_id')->select();

      $this->success('','',$list);
    }

    // 生成订单
    public function createorder()
    {
      if($this->request->isPost()){
        $param = $this->request->param();
        $xd_time = date('Y-m-d H:i:s', time());

        $data=[
          'user_id' => $param['userid'],
          'order_num' => build_code('YD'),
          'status' => 1,
          'time' => (string)$xd_time,
          'ps_date' => $param['ps_date'],
          'ps_time_id' => intval($param['ps_time_id']),
          'contact_name' => $param['contact_name'],
          'contact_phone' => $param['contact_phone'],
          'note' => $param['note'],
          'is_del' => 2,
          'ys_request' => $param['ys_request'],
          'airline_id' => $param['airline_id'],
          'start_point_id' => $param['start_point_id'],
          'end_point_id' => $param['end_point_id'],
          'item_type_id' => $param['item_type_id'],
          'item_nums' => intval($param['item_nums']),
          'item_weight' => intval($param['item_weight']),
          'is_urgent' => $param['is_urgent'],
          'item_unit_id' => $param['item_unit_id']
        ];

        $status=$this->OrderModel->save($data);
        if ($status) {
            $this->success('预约下单成功');
        } else {
            $this->error($this->OrderModel->getError());
        }
      }
    }

    // 用户订单列表
    public function yhlist(){
      $userid = $this->request->param('userid', '', 'trim');
      $status = $this->request->param('status', 0, 'trim');

      if($status==='0'){
        $list=$this->OrderModel
        ->with(['airline','startPoint','endPoint','type','timelist','unit'])
        ->where('user_id',$userid)->order('time desc')->select();
      }else{
        $list=$this->OrderModel
        ->with(['airline','startPoint','endPoint','type','timelist','unit'])
        ->where(['user_id'=>$userid,'status'=>$status])->order('time desc')->select();
      }

      $this->success('','',$list);
    }
    
    // 订单详情
    public function orderinfo(){
      $order_id = $this->request->param('orderid', '', 'trim');

      $data=$this->OrderModel
      ->with(['airline','startPoint','endPoint','type','timelist','unit','uav'])
      ->where('order.id',$order_id)->find();

      $this->success('','',$data);
    }
        
    // 取消订单
    public function cancel(){
      $orderid = $this->request->param('orderid', '', 'trim');
      $cancel_reason = $this->request->param('cancel_reason', '', 'trim');

      $data=[
        'id'=>$orderid,
        'status'=>5,
        'cancel_reason'=>$cancel_reason
      ];

      $result=$this->OrderModel->isUpdate(true)->save($data);
      if($result === false){
          $this->error($this->OrderModel->getError());
      }else {
          $this->success('取消订单申请成功');
      }
    }

    // 确认收货
    public function qrsh(){
      $orderid = $this->request->param('orderid', '', 'trim');

      $data=[
        'id'=>$orderid,
        'status'=>3,
      ];

      $result=$this->OrderModel->isUpdate(true)->save($data);
      if($result === false){
          $this->error($this->OrderModel->getError());
      }else {
          $this->success('确认收货成功');
      }
    }
}
